package com.cloud.server.user.service;

import com.cloud.core.db.entity.ConfigAdEntity;
import com.cloud.core.db.entity.UserInfoEntity;
import com.cloud.core.db.mapper.ConfigAdMapper;
import com.cloud.core.db.mapper.UserInfoMapper;
import com.cloud.core.param.server.req.UserReq;
import com.cloud.server.user.helper.UserHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by cloudy.xie on 17/12/14.
 */
@Component
public class UserService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    public UserInfoEntity login(UserReq userReq) throws SQLException {
        UserInfoEntity entity = UserHelper.userParamToEntity(userReq);
        String loginType = userReq.getLoginType();

        LoginTypeEnum login_type = LoginTypeEnum.valueOf(loginType);
        int count = userInfoMapper.count(entity);
        if(count == 0){
            switch (login_type){
                case WEIXIN : userInfoMapper.insert(entity); break;
                case WEIBO :  userInfoMapper.insert(entity); break;
                default: return null;
            }
        }

        List<UserInfoEntity> userList = userInfoMapper.selectByExample(entity);
        if(CollectionUtils.isEmpty(userList)){
            return null;
        }
        return userList.get(0);
    }
}
